import router from "./router";

import store from "./store";

router.beforeEach((to, from, next) => {
    let token = store.state.user.token
    let userInfo = store.state.user.userInfo
    // console.log(token);

    if (token) {
        if (to.path == '/login') {
            next(from.path)
        } else {
            if(!userInfo){
                store.dispatch('getUserInfo').then(res=>{
                    // console.log(res);
                    next()
                }).catch(error=>{
                    next('/login')
                    console.log(error);
                })
            }else{
                next()
            }
        }
    } else {
        if (to.path == '/login') {
            next()
        } else {
            next('/login')
        }
    }
})